﻿/// <reference path="../Extension.js" />

var elem, select;
$(document).ready(function () {
    $('.Accordion').accordion({
        collapsible: true,
        active: false,
        autoHeight: false,
        navigation: true,
        change: function (event, ui) {
            if (ui.newHeader[0] == null || ui.newHeader[0].innerText != 'Endereços') {
                $('.also').first().width(560);
                $('.field580').first().width(580);
            }
            else {
                $('.also').first().width(960);
                $('.field580').first().width(980);
            }
            $('.Accordion').accordion('resize');
        }
    });

    $("#tTelefone").jqGrid({
        datatype: 'json',
        url: '/Pessoas/FonesList',
        mtype: 'GET',
        colNames: ['', 'Id', 'Telefone', 'Observações'],
        colModel: [
            { name: "actions", formatter: "actions", editable: false, sortable: false, resizable: false, fixed: true, align: 'center', width: 26, "formatoptions": { keys: false, editbutton: false, delbutton: true} },
            { name: 'ID_FONE', index: 'ID_FONE', width: 80, sortable: false, align: 'right', search: false, hidden: true },
            { name: 'NUMERO_FONE', index: 'NUMERO_FONE', width: 140, align: 'center', editable: true, editoptions: { dataInit: function (elem) { $(elem).setMask({ mask: '(99) 9999-9999', autoTab: false }); } } },
            { name: 'OBS_FONE', index: 'OBS_FONE', width: 360, align: 'left', editable: true, searchoptions: { sopt: ['cn', 'eq']} }
        ],
        gridComplete: function () { loadInputs(); },
        pager: '#pTelefone',
        height: 'auto',
        width: 500,
        rowNum: 5,
        rowList: [5, 10, 15, 20],
        sortname: 'NUMERO_FONE',
        sortorder: "asc",
        autowidth: false,
        viewrecords: true,
        viewsortcols: true,
        gridview: true,
        multiselect: false,
        editurl: '/Pessoas/FoneDelete',
        postData: {
            ID_PESSOA: function () {
                return $('#ID_PESSOA').val();
            }
        },
        ondblClickRow: function (id) {
            editRow('tTelefone', '/Pessoas/FoneUpdate', id, { ID_FONE: id });
        }
    }).navGrid("#pTelefone", {
        edit: false, add: false, del: false, search: false, refresh: true
    }).navButtonAdd("#pTelefone", {
        caption: '',
        buttonicon: 'ui-icon-circle-plus',
        onClickButton: function () {
            $("#tTelefone").addRowData(0, {
                ID_FONE: '', NUMERO_FONE: '', OBS_FONE: ''
            }, 'first');
            addRow('tTelefone', '/Pessoas/FoneUpdate', { ID_PESSOA: $('#ID_PESSOA').val() });
        },
        title: "adicionar telefone",
        position: "last"
    });

    $("#tEmail").jqGrid({
        datatype: 'json',
        url: '/Pessoas/EmailsList',
        mtype: 'GET',
        colNames: ['', 'Id', 'E-mail', 'Observações'],
        colModel: [
                { name: "actions", formatter: "actions", editable: false, sortable: false, resizable: false, fixed: true, align: 'center', width: 26, "formatoptions": { keys: false, editbutton: false, delbutton: true} },
                { name: 'ID_EMAIL', index: 'ID_EMAIL', width: 80, sortable: false, align: 'right', search: false, hidden: true },
                { name: 'ENDERECO_EMAIL', index: 'ENDERECO_EMAIL', width: 300, align: 'left', editable: true },
                { name: 'OBS_EMAIL', index: 'OBS_EMAIL', width: 200, align: 'left', editable: true, searchoptions: { sopt: ['cn', 'eq']} }
            ],
        altRows: false,
        pager: $('#pEmail'),
        height: 'auto',
        width: 500,
        rowNum: 5,
        rowList: [5, 10, 15, 20],
        sortname: 'ID_EMAIL',
        sortorder: "asc",
        autowidth: false,
        viewrecords: true,
        viewsortcols: true,
        gridview: true,
        multiselect: false,
        editurl: '/Pessoas/EmailDelete',
        postData: {
            ID_PESSOA: function () { return $('#ID_PESSOA').val(); }
        },
        ondblClickRow: function (id) {
            editRow('tEmail', '/Pessoas/EmailUpdate', id, { ID_EMAIL: id });
        }
    }).navGrid("#pEmail", {
        edit: false, add: false, del: false, search: false, refresh: true
    }).navButtonAdd("#pEmail", {
        caption: '',
        buttonicon: 'ui-icon-circle-plus',
        onClickButton: function () {
            $("#tEmail").addRowData(0, {
                ID_EMAIL: '', ENDERECO_EMAIL: '', OBS_EMAIL: ''
            }, 'first');
            addRow('tEmail', '/Pessoas/EmailUpdate', { ID_PESSOA: $('#ID_PESSOA').val() });
        },
        title: "adicionar email",
        position: "last"
    });

    $("#tEndereco").jqGrid({
        datatype: 'json',
        url: '/Pessoas/EnderecosList',
        mtype: 'GET',
        colNames: ['Pessoa', 'ENPS', 'Tipo', 'Endereço', 'Bairro', 'Cidade', 'UF', 'CEP', 'Lograd'],
        colModel: [
            { name: 'ID_PESSOA', index: 'ID_PESSOA', width: 40, sortable: false, align: 'right', search: false, hidden: true },
            { name: 'ID_ENPS', index: 'ID_ENPS', width: 40, sortable: false, align: 'right', search: false, hidden: true },
            { name: 'DESC_TPEN', index: 'DESC_TPEN', width: 100, sortable: false, align: 'left', search: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'LOGR_END', index: 'LOGR_END', width: 320, sortable: false, align: 'left', search: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'NOME_BAIRRO', index: 'NOME_BAIRRO', width: 180, align: 'left', search: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'NOME_CID', index: 'NOME_CID', width: 160, align: 'left', search: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'SIGLA_UF', index: 'SIGLA_UF', width: 40, align: 'center', search: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'CEP_END', index: 'CEP_END', width: 100, align: 'center', search: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'NOME_LOGRAD', index: 'NOME_LOGRAD', width: 80, sortable: false, align: 'left', hidden: true }
        ],
        altRows: false,
        pager: $('#pEndereco'),
        height: 'auto',
        rowNum: 5,
        rowList: [5, 10, 15, 20],
        sortname: 'ID_ENPS',
        sortorder: "asc",
        postData: {
            ID_PESSOA: function () { return $('#ID_PESSOA').val(); }
        },
        ondblClickRow: function (id) {
            jqGridEdit(
                'Editar dados do endereço',
                'tEndereco',
                '__enderecoedit',
                '/Enderecos/Edit',
                '/Enderecos/Update',
                id,
                null,
                'EnderecoEditForm',
                function () {
                    $('#CEP_END').setMask({ mask: '99999-999', autoTab: false });
                    $('#ID_UF').change(function () {
                        CidadeSearch($(this).val());
                    });
                    $('#ID_CID').change(function () {
                        BairroSearch($(this).val());
                    });
                    $('#LOGR_END').autocomplete({
                        source: function (request, response) {
                            $.ajax({
                                url: '/Enderecos/LookupList',
                                datatype: "json",
                                data: {
                                    term: request.term,
                                    ID_FNPR: 8572,
                                    ID_UF: $('#ID_UF').val(),
                                    ID_CID: $('#ID_CID').val(),
                                    ID_BAIRRO: $('#ID_BAIRRO').val()
                                },
                                success: function (data) {
                                    response($.map(data, function (item) {
                                        return {
                                            id: item.id,
                                            label: item.label,
                                            value: item.value,
                                            ID_END: item.ID_END,
                                            ID_LOGRAD: item.ID_LOGRAD,
                                            ID_BAIRRO: item.ID_BAIRRO
                                        }
                                    }))
                                }
                            })
                        },
                        minLength: 4,
                        select: function (event, ui) {
                            $('#CEP_END').val(ui.item.id);
                            $('#ID_END').val(ui.item.ID_END);
                            $('#ID_LOGRAD').val(ui.item.ID_LOGRAD);
                            $('#ID_BAIRRO').val(ui.item.ID_BAIRRO);
                        }
                    });
                },
                null,
                null,
                null,
                'ID_PESSOA=' + $('#ID_PESSOA').val()
            );
        }
    }).navGrid("#pEndereco", {
        edit: false, add: false, del: false, search: false, refresh: false
    }).navButtonAdd('#pEndereco', {
        caption: '',
        title: 'adicionar endereço',
        buttonicon: "ui-icon-plus",
        onClickButton: function () {
            jqGridEdit(
                    'Editar dados do endereço',
                    'tEndereco',
                    '__enderecoedit',
                    '/Enderecos/Edit',
                    '/Enderecos/Update',
                    null,
                    880,
                    'EnderecoEditForm',
                    function () {
                        $('#CEP_END').setMask({ mask: '99999-999', autoTab: false });
                        $('#ID_UF').change(function () {
                            CidadeSearch($(this).val());
                        });
                        $('#ID_CID').change(function () {
                            BairroSearch($(this).val());
                        });
                        $('#LOGR_END').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    url: '/Enderecos/LookupList',
                                    datatype: "json",
                                    data: {
                                        term: request.term,
                                        ID_FNPR: 8572,
                                        ID_UF: $('#ID_UF').val(),
                                        ID_CID: $('#ID_CID').val(),
                                        ID_BAIRRO: $('#ID_BAIRRO').val()
                                    },
                                    success: function (data) {
                                        response($.map(data, function (item) {
                                            return {
                                                id: item.id,
                                                label: item.label,
                                                value: item.value,
                                                ID_END: item.ID_END,
                                                ID_LOGRAD: item.ID_LOGRAD,
                                                ID_BAIRRO: item.ID_BAIRRO
                                            }
                                        }))
                                    }
                                })
                            },
                            minLength: 4,
                            select: function (event, ui) {
                                $('#CEP_END').val(ui.item.id);
                                $('#ID_END').val(ui.item.ID_END);
                                $('#ID_LOGRAD').val(ui.item.ID_LOGRAD);
                                $('#ID_BAIRRO').val(ui.item.ID_BAIRRO);
                            }
                        });
                    },
                    null,
                    null,
                    null,
                    '&ID_PESSOA=' + $('#ID_PESSOA').val()
                );
        },
        position: "last"
    }).navButtonAdd('#pEndereco', {
        caption: '',
        title: 'excluir endereço selecionado',
        buttonicon: "ui-icon-trash",
        onClickButton: function () {
            jqGridDelete('tEndereco', '/Pessoas/EnderecoDelete', 'ID_ENPS', $('#ID_FNPRENDERECODELETE').val());
        },
        position: 'last'
    }).navSeparatorAdd("#pEndereco", {
        sepclass: 'ui-separator',
        sepcontent: ''
    }).navButtonAdd('#pEndereco', {
        caption: '',
        title: 'localizar registros',
        buttonicon: 'ui-icon-search',
        onClickButton: function () {
            $('#tEndereco').searchGrid({ closeAfterSearch: true });
        },
        position: 'last'
    }).navButtonAdd('#pEndereco', {
        caption: '',
        title: 'recarregar tabela',
        buttonicon: 'ui-icon-refresh',
        onClickButton: function () {
            $('#tEndereco').trigger('reloadGrid');
        },
        position: 'last'
    }).navButtonAdd('#pEndereco', {
        caption: '',
        title: '',
        buttonicon: "ui-icon-image",
        onClickButton: function () {
            enderecoMap($("#tEndereco").getRowData($("#tEndereco").getGridParam('selrow'))['ID_ENPS']);
        },
        position: "last"
    });
});